<?php

namespace App\Models\OpenShop\ShopEngineeringConstruction;

use App\Enums\OpenShop\InvestmentPromotion\MainProcessEnum;
use App\Models\BaseModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUploadFileResourceModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserBaseInfoModel;
use App\Support\ChangeSupport;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class OpenShopEngineeringConstructionModel extends BaseModel
{
    use HasFactory;

    public static array $orderLogTranslate = [
        "engineeringConstructionStartTime"  => [
            "type" => ChangeSupport::FILED_TYPE_DATE_YMD,
            "desc" => "开始时间"
        ],
        "finishStatus"                      => [
            "type" => ChangeSupport::FILED_TYPE_ENUM,
            "desc" => "完成状态",
            "enum" => OpenShopEngineeringConstructionModel::FINISH_STATUS_MAP
        ],
        "engineeringConstructionFinishTime" => [
            "type" => ChangeSupport::FILED_TYPE_DATE_YMD,
            "desc" => "结束时间"
        ],
        "step"                              => [
            "type" => ChangeSupport::FILED_TYPE_ENUM,
            "desc" => "步骤",
            "enum" => MainProcessEnum::MAIN_PROCESS_ENGINEER_CONSTRUCT_MAP
        ]
    ];

    const FINISH_STATUS_NO   = 1;
    const FINISH_STATUS_YES  = 2;
    const FINISH_STATUS_STAY = 3;

    const FINISH_STATUS_MAP = [
        self::FINISH_STATUS_NO   => "不通过",
        self::FINISH_STATUS_YES  => "通过",
        self::FINISH_STATUS_STAY => "通过待调整"
    ];

    protected $table = "open_shop_engineering_construction";

    protected $fillable = [
        'id',
        'mainProcessId',
        'orderNo',
        'step',
        'engineeringConstructionStartTime',
        'engineeringConstructionFinishTime',
        'finishStatus',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'updatedByUid',
    ];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format("Y-m-d H:i:s");
    }

    /**
     * @return HasMany
     */
    public function attachRel(): HasMany
    {
        return $this->hasMany(OpenShopUploadFileResourceModel::class, "orderNo", "orderNo");
    }

    /**
     * @return HasOne
     */
    public function mainProcessRel(): HasOne
    {
        return $this->hasOne(OpenShopMainProcessModel::class, 'id', 'mainProcessId');
    }

    /**
     * @return HasOne
     */
    public function baseInfoRel(): HasOne
    {
        return $this->hasOne(OpenShopUserBaseInfoModel::class, "orderNo", "orderNo");
    }

}
